package com.lancoo.znbkxx.teacher.aibkGenerateTestPaper.aibkCustomTemplet

import android.text.Html
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import com.google.gson.Gson
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkGenerateTestPaper.aibkGenerateDialog.DialogTempletQuesTypeChoose
import com.lancoo.znbkxx.teacher.aibkGenerateTestPaper.aibkGeneratePaper.itemtouchhelper.IItemTouchHelperAdapter
import com.lancoo.znbkxx.teacher.aibkGenerateTestPaper.aibkGeneratePaper.itemtouchhelper.OnStartDragListener
import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkTools.LancooMethod
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerAdapter
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerViewHolder
import com.lancoo.znbkxx.teacher.utils.ButtonUtils
import kotlinx.android.synthetic.main.aibk_adapter_item_questype.view.*
import java.util.*

class AibkCustomTempletAdapter(
        val activity: AppCompatActivity,
        var customTemplet: CustomTemplet?,
        val dragListener: OnStartDragListener
) : KBaseRecyclerAdapter(), IItemTouchHelperAdapter {

    override fun getHolderView(parent: ViewGroup?, position: Int): View? {
        return LayoutInflater.from(activity)
                .inflate(R.layout.aibk_adapter_item_questype, parent, false)
    }

    override fun onBindViewHolder(holder: KBaseRecyclerViewHolder, position: Int) {
        val typeNo = customTemplet!!.Content[holder.layoutPosition].TypeNo
        var genreName = customTemplet!!.Content[holder.layoutPosition].GenreName
        if(genreName.isEmpty()) {
            genreName = customTemplet!!.Content[holder.layoutPosition].TypeName
        }
        val quesListSize = customTemplet!!.Content[holder.layoutPosition].QuesList.size
        var itemCountAll = 0
        var eachScoreX = ""
        for(i in 0 until quesListSize){
            val itemNum = customTemplet!!.Content[holder.layoutPosition].QuesList[i].ItemNum
            val eachScore = customTemplet!!.Content[holder.layoutPosition].QuesList[i].ScoreList[0]
            eachScoreX = LancooMethod.killlingDouble(eachScore).toString()
            itemCountAll += itemNum
        }

        if(typeNo == "A"){
            holder.convertView.IvQuesTypeIcon.setImageResource(R.mipmap.aibk_icon_tick_green)
        }else if(typeNo == "D"){
            holder.convertView.IvQuesTypeIcon.setImageResource(R.mipmap.aibk_icon_tick_blue)
        }else if(typeNo == "m"||typeNo == "C"){
            holder.convertView.IvQuesTypeIcon.setImageResource(R.mipmap.aibk_icon_tick_orange)
        }else if(typeNo == "X"){
            holder.convertView.IvQuesTypeIcon.setImageResource(R.mipmap.aibk_icon_pen_green)
        }else if(typeNo == "N"){
            holder.convertView.IvQuesTypeIcon.setImageResource(R.mipmap.aibk_icon_pen_blue)
        }else if(typeNo == "Q"||typeNo == "b"||typeNo == "d"||typeNo == "v"||typeNo == "g"||typeNo == "f"){
            holder.convertView.IvQuesTypeIcon.setImageResource(R.mipmap.aibk_icon_pen_orange)
        }else if(typeNo == "E"||typeNo == "F"){
            holder.convertView.IvQuesTypeIcon.setImageResource(R.mipmap.aibk_icon_earphones_green)
        }else if(typeNo == "S"){
            holder.convertView.IvQuesTypeIcon.setImageResource(R.mipmap.aibk_icon_earphones_blue)
        }else if(typeNo == "x"){
            holder.convertView.IvQuesTypeIcon.setImageResource(R.mipmap.aibk_icon_earphones_orange)
        }else{
            holder.convertView.IvQuesTypeIcon.setImageResource(R.mipmap.aibk_icon_synthesis_blue)
        }

        holder.convertView.TvQuesTypeName.text = genreName

        holder.convertView.TvQuesTypeInfor.text =
                Html.fromHtml(activity.resources.getString(R.string.aibk_templet_questype_infor, quesListSize, itemCountAll, eachScoreX))


        //模板拖动交换
        holder.convertView.LlItemView.setOnLongClickListener(View.OnLongClickListener {
            dragListener.onStartDrag(holder)//此处屏蔽，即为屏蔽拖动排序事件
            false
        })

        holder.convertView.LlItemView.setOnClickListener {
            if (!ButtonUtils.isFastDoubleClick(R.id.BtnListTemplateAdd)) {
                onSwipeListener!!.onTempletRevise(customTemplet!!,holder.adapterPosition)
            }
        }

        //模板删除
        holder.convertView.IvDelete.setOnClickListener(View.OnClickListener {
            if (null != onSwipeListener) {
                //且如果想让侧滑菜单同时关闭，需要同时调用
                //((SwipeMenuLayout) holder.itemView).quickClose();
                customTemplet!!.Content.removeAt(holder.adapterPosition)
                onSwipeListener!!.onTempletDelete(holder.itemView, holder.adapterPosition)
                notifyItemRemoved(holder.adapterPosition)
                Log.e("==显示==", "===========最新删除的position===============$position")
            }
        })
    }

    override fun getItemCount(): Int {
        var itemSize = 0
        if(customTemplet!=null&&customTemplet!!.Content!=null&&customTemplet!!.Content.size>0){
            itemSize = customTemplet!!.Content.size
        }
        return itemSize
    }

    fun refreshView(customTempletX: CustomTemplet){
        customTemplet = customTempletX
        notifyDataSetChanged()
    }

    override fun onItemDismiss(position: Int) {
        //不做操作，但不能删除
    }

    override fun onItemMove(fromPosition: Int, toPosition: Int) {
        Collections.swap(customTemplet!!.Content, fromPosition, toPosition)
        notifyItemMoved(fromPosition, toPosition)
    }

    interface OnSwipeListener {
        fun onTempletDelete(view: View?, pos: Int)//删除模板
        fun onTempletRevise(customTemplet:CustomTemplet,pos: Int)//修改模板
    }

    private var onSwipeListener: OnSwipeListener? = null

    fun setOnDelListener(mOnDelListener: OnSwipeListener?) {
        onSwipeListener = mOnDelListener
    }
}